package com.tudouji.project.flow.controller;

import com.flow.service.pojo.BasePojo;
import com.flow.service.pojo.ResultParam;
import com.flow.service.pojo.flow.FlowTask;
import com.flow.service.pojo.flow.FlowTempNode;
import com.flow.service.pojo.result.FlowResult;
import com.flow.service.pojo.user.FlowUser;
import com.flow.service.process.service.IFlowTempNodeService;
import com.flow.service.process.service.impl.FlowProcessProxy;
import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.project.flow.utils.FlowProcessUtil;
import com.tudouji.project.purchasebill.domain.Purchasebill;
import com.tudouji.project.purchasebill.service.IPurchasebillService;
import com.tudouji.project.store.domain.Storeroom;
import com.tudouji.project.store.service.IStoreroomService;
import com.tudouji.project.system.domain.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.Action;
import java.util.List;

/**
 * @author xiaxl
 * @version v1.0
 * @ClassName FlowTempNodesController
 * @Descrption
 * @date 2022/4/20 15:23
 */
@Slf4j
@RestController
@RequestMapping("/node")
public class FlowTempNodesController extends BaseController {

    @Autowired
    private FlowProcessUtil flowProcessUtil;
    @Autowired
    private FlowProcessProxy flowProcessProxy;

    @Autowired
    @Qualifier("flowTempNodeService")
    private IFlowTempNodeService flowTempNodeService;

    /**
     * 获取审批流程
     * @return
     */
    @GetMapping("/getNodes")
    public AjaxResult getNodes(Long orderid, Long flowid, Long orgId , Long deptId, @RequestParam(required = false) Long platformId) {
        BasePojo base=null;

        List<Object> list = flowProcessUtil.getApprovalRecord(orderid, flowid,base);
        return AjaxResult.success(list);
    }

    @GetMapping("/getFlowtempnode")
    public AjaxResult getFlowtempnode(FlowTempNode flowtempnode) {
        List<FlowTempNode> list = flowTempNodeService.selectFlowTempNodeList(flowtempnode);
        return AjaxResult.success(list);
    }
    /**
     * 获取可拒绝节点
     * @param orderid
     * @param taskid
     * @return
     */
    @GetMapping("/getRefuseNodes")
    public AjaxResult getRefuseNodes(Long orderid,Long taskid) {
        log.info("----------orderid------"+orderid+"----"+taskid);
        SysUser user= SecurityUtils.getLoginUser().getUser();
        List<com.flow.service.pojo.flow.FlowTempNode> list = flowProcessUtil.getRefuseNodes(orderid, taskid);
        return AjaxResult.success(list);
    }

    /**
     * 转交
     * @param taskId 当前taskId
     * @param toUserId 转交对象id
     * @return
     */
    @PostMapping("/transmit")
    public AjaxResult transmit(Long taskId,Long toUserId,String opinion,String fileIds){
        SysUser user= SecurityUtils.getLoginUser().getUser();
        FlowResult result = flowProcessUtil.defaultTransmit(taskId, user, toUserId, opinion, fileIds);
        if(result.getMsg().equals(ResultParam.SUCCESS)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error(result.getMsg());
        }
    }
    @GetMapping("/getHandleNodes")
    public AjaxResult getHandleNodes(Long orderid,Long flowId) {
        List<com.flow.service.pojo.flow.FlowTempNode> list = flowProcessUtil.getHandleNodes(orderid, flowId);
        return AjaxResult.success(list);
    }
    /**
     * 获取 起始节点
     * @return
     */
    @GetMapping("/getStartNodeByFlowId")
    public AjaxResult getStartNodeByFlowId(Long flowId) {
        FlowTempNode node  = flowTempNodeService.selectStartNodeByFlowId(flowId);
        return AjaxResult.success(node);
    }

    @GetMapping("/selectOnesUndoTasks")
    public AjaxResult selectOnesUndoTasks(FlowTask flowTask) {
        SysUser user= SecurityUtils.getLoginUser().getUser();
        flowTask.setRecieverId(user.getUserId());
        List<FlowTempNode>  list = flowProcessProxy.selectOnesUndoTasks(flowTask);
        return AjaxResult.success(list);
    }

}
